﻿using GalaSoft.MvvmLight;
using WP7EmulatorSwitcher.Model;
using System.Collections.Generic;
using System.Linq;

namespace WP7EmulatorSwitcher.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IPhoneSkinService _skinService;

        /// <summary>
        /// The <see cref="WelcomeTitle" /> property's name.
        /// </summary>
        public const string WelcomeTitlePropertyName = "WelcomeTitle";

        private string _welcomeTitle = "WP7 Emulator Skin Switcher";

        /// <summary>
        /// Gets the WelcomeTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string WelcomeTitle
        {
            get
            {
                return _welcomeTitle;
            }

            set
            {
                if (_welcomeTitle == value)
                {
                    return;
                }

                _welcomeTitle = value;
                RaisePropertyChanged(WelcomeTitlePropertyName);
            }
        }


        /// <summary>
        /// The <see cref="PhoneList" /> property's name.
        /// </summary>
        public const string PhoneListPropertyName = "PhoneList";

        private List<PhoneSkin> _phoneList = new List<PhoneSkin>();

        /// <summary>
        /// Sets and gets the PhoneList property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public List<PhoneSkin> PhoneList
        {
            get
            {
                return _phoneList;
            }

            set
            {
                if (_phoneList == value)
                {
                    return;
                }

                _phoneList = value;
                RaisePropertyChanged(PhoneListPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SelectedPhone" /> property's name.
        /// </summary>
        public const string SelectedPhonePropertyName = "SelectedPhone";

        private PhoneSkin _selectedPhone = null;

        /// <summary>
        /// Sets and gets the SelectedPhone property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public PhoneSkin SelectedPhone
        {
            get
            {
                return _selectedPhone;
            }

            set
            {
                if (_selectedPhone == value)
                {
                    return;
                }

                _selectedPhone = value;
                ChangeEmulatorPhoneSkin(_selectedPhone);
                RaisePropertyChanged(SelectedPhonePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="LogMessages" /> property's name.
        /// </summary>
        public const string LogMessagesPropertyName = "LogMessages";

        private string _logMessages = "";

        /// <summary>
        /// Sets and gets the LogMessages property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string LogMessages
        {
            get
            {
                return _logMessages;
            }

            set
            {
                if (_logMessages == value)
                {
                    return;
                }

                _logMessages = value;
                RaisePropertyChanged(LogMessagesPropertyName);
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IPhoneSkinService dataService)
        {
            _skinService = dataService;
            _skinService.GetPhoneSkins(
                (phoneSkins, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }
                    PhoneList = phoneSkins.ToList();
                });
        }

        private void ChangeEmulatorPhoneSkin(PhoneSkin _selectedPhone)
        {
            LogMessages += "Changing emulator skin to " + _selectedPhone.Title + " " +_selectedPhone.SubTitle + ".";
            bool skinSwitched = _skinService.SetEmulatorPhoneSkin(_selectedPhone);

            if(skinSwitched)
            {
                LogMessages = "Skin switched succesfully to " + _selectedPhone.Title + " " +_selectedPhone.SubTitle + ".";;
            }
            else
            {
                LogMessages += "Error switching skins. (Maybe emulator is still running?)";
            }
        }
    }
}